<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);

$exc = new exchange($ecs->table("service_zc"), $db, 'id', 'title');

/*------------------------------------------------------ */
//-- 列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      $_LANG['16_service_zc_list']);
    $smarty->assign('action_link',  array('text' => $_LANG['service_zc_add'], 'href'=>'rg_service_zc.php?act=add'));
    $smarty->assign('full_page',  1);

    $service_zcs_list = service_zcs_list();

    $smarty->assign('service_zcs_list',   $service_zcs_list['service_zcs_list']);
    $smarty->assign('filter',       $service_zcs_list['filter']);
    $smarty->assign('record_count', $service_zcs_list['record_count']);
    $smarty->assign('page_count',   $service_zcs_list['page_count']);

    assign_query_info();
    $smarty->display('rg_service_zc_list.htm');
}
/*------------------------------------------------------ */
//-- ajax 列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $service_zcs_list = service_zcs_list();
    $smarty->assign('service_zcs_list',    $service_zcs_list['service_zcs_list']);
    $smarty->assign('filter',       $service_zcs_list['filter']);
    $smarty->assign('record_count', $service_zcs_list['record_count']);
    $smarty->assign('page_count',   $service_zcs_list['page_count']);

    $sort_flag  = sort_flag($service_zcs_list['filter']);

    make_json_result($smarty->fetch('rg_service_zc_list.htm'), '', array('filter' => $service_zcs_list['filter'], 'page_count' => $service_zcs_list['page_count']));
}
/*------------------------------------------------------ */
//-- 添加
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('service_zc');

    $smarty->assign('ur_here',      $_LANG['service_zc_add']);
    $smarty->assign('form_action','insert');
    $smarty->assign('action_link',  array('text' => $_LANG['16_service_zc_list'], 'href'=>'rg_service_zc.php?act=list'));

    assign_query_info();
    $smarty->display('rg_service_zc_info.htm');
}
if ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('service_zc');

    /*检查是否重复*/
    $is_only = $exc->is_only('title', $_POST['title']);

    if (!$is_only)
    {
        sys_msg(sprintf($_LANG['service_zcname_exist'], stripslashes($_POST['title'])), 1);
    }

    /*插入数据*/
    $sql = "INSERT INTO ".$ecs->table('service_zc')." (title, link, zc_desc) VALUES ('".$_POST[title]."', '".$_POST[link]."', '".$_POST[zc_desc]."')";
    $db->query($sql);

    /*添加链接*/
    $link[0]['text'] = $_LANG['back_list'];
    $link[0]['href'] = 'rg_service_zc.php?act=list';
    $link[1]['text'] = $_LANG['continue_add'];
    $link[1]['href'] = 'rg_service_zc.php?act=add';
    sys_msg($_POST['service_zc_name'].$_LANG['service_zcadd_succed'],0, $link);
    admin_log($_POST['title'],'add','service_zc');

}

/*------------------------------------------------------ */
//-- 编辑
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('service_zc');

    $sql = "SELECT * FROM " .$ecs->table('service_zc'). " WHERE id='$_REQUEST[id]'";
    $service_zc = $db->GetRow($sql);
    $smarty->assign('ur_here',      $_LANG['service_zc_edit']);
    $smarty->assign('action_link',  array('text' => $_LANG['16_service_zc_list'], 'href'=>'rg_service_zc.php?act=list&' . list_link_postfix()));
    $smarty->assign('service_zc',       $service_zc);
    $smarty->assign('form_action','update');
    $smarty->display('rg_service_zc_info.htm');
}
if ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('service_zc');
    if ($_POST['title'] != $_POST['old_title'])
    {
        /*检查是否相同*/
        $is_only = $exc->is_only('title', $_POST['title'], $_POST['id']);

        if (!$is_only)
        {
            sys_msg(sprintf($_LANG['service_zcname_exist'], stripslashes($_POST['title'])), 1);
        }
    }

    $param = "title = '$_POST[title]', zc_desc = '$_POST[zc_desc]', link = '$_POST[link]' ";

    if ($exc->edit($param ,  $_POST['id']))
    {
        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'rg_service_zc.php?act=list&' . list_link_postfix();
        $note = sprintf($_LANG['service_zcedit_succed'], $_POST['title']);
        sys_msg($note, 0, $link);
        admin_log($_POST['title'], 'edit', 'service_zc');
    }
    else
    {
        die($db->error());
    }

}

/*------------------------------------------------------ */
//-- 编辑名称
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_name')
{
    check_authz_json('service_zc');

    $id = intval($_POST['id']);
    $val = json_str_iconv(trim($_POST['val']));

    $service_zc_name = $exc->get_name($id);

    if (!$exc->is_only('title', $val, $id))
    {
        make_json_error(sprintf($_LANG['service_zcname_exist'], $service_zc_name));
    }
    else
    {
        $exc->edit("title='$val'", $id);

        admin_log($val, 'edit', 'service_zc');
        make_json_result(stripslashes($val));
    }
}


/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'remove')
{
    check_authz_json('service_zc');

    $id     = intval($_GET['id']);
    $name   = $exc->get_name($id);

    if ($exc->drop($id))
    {
        admin_log(addslashes($name),'remove','service_zc');

        $url = 'rg_service_zc.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($_LANG['service_zcremove_falure']);
        return false;
    }
}

function service_zcs_list()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']      = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']   = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('service_zc');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('service_zc').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $service_zcs_list = $GLOBALS['db']->getAll($sql);

    $arr = array('service_zcs_list' => $service_zcs_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>